package drive.Mapper;

import drive.Model.ChatSession;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ChatSessionMapper {
    // 查询所有聊天会话
    @Select("SELECT * FROM chatsession")
    List<ChatSession> findAll();

    // 根据chatSessionID查询聊天会话
    @Select("SELECT * FROM chatsession WHERE chatSessionID = #{chatSessionID}")
    ChatSession findByChatSessionID(int chatSessionID);

    // 根据user1ID和user2ID查询聊天会话
    @Select("SELECT * FROM chatsession WHERE user1ID = #{user1ID} AND user2ID = #{user2ID}")
    ChatSession findByUserIDs(int user1ID, int user2ID);

    // 新增聊天会话
    @Insert("INSERT INTO chatsession (user1ID, user2ID, creationTime,updateTime) VALUES (#{user1ID}, #{user2ID}, #{creationTime}, #{updateTime})")
    int add(ChatSession chatSession);
    // 新增商品聊天会话
    @Insert("INSERT INTO chatsession (user1ID, user2ID, creationTime, itemId, updateTime) VALUES (#{user1ID}, #{user2ID}, #{creationTime}, #{itemId}, #{updateTime})")
    int add_item_session(ChatSession chatSession);

    // 更新聊天会话
    @Update("UPDATE chatsession SET user1ID = #{user1ID}, user2ID = #{user2ID}, updateTime = #{updateTime} WHERE chatSessionID = #{chatSessionID}")
    int update(ChatSession chatSession);

    @Update("UPDATE chatsession SET updateTime = NOW() WHERE chatSessionID = #{chatSessionID}")
    int update_time(int chatSessionID);
    // 删除聊天会话
    @Delete("DELETE FROM chatsession WHERE chatSessionID = #{chatSessionID}")
    int delete(int chatSessionID);

    /**
     * 根据用户ID查找所有相关的会话
     * @param userId 用户ID
     * @return 包含会话信息的列表
     */
    @Select("SELECT * FROM chatsession WHERE user1ID = #{userId} OR user2ID = #{userId} ORDER BY updateTime")
    List<ChatSession> findByUserId(int userId);
}